
var netUtil = require('/../../utils/netUtil.js');
var util = require('/../../utils/util.js');
var app = getApp();
Page({
  data:{
 		sendSmsCodeText : '发送验证码',
 		smsCodeBoxShow : false,
 		verifyCode : '',
 		phoneNum : '',
 		sendSmsInteval : null, 
 		pageWhereToGo : '',
 		swiperImageList : [],   //页顶滚图地址数组
 		newsItemList : []      //新闻地址数组
  },
  onLoad:function(options){
  	this.getSwiperImageList();
  },
  onReady:function(){
  
  },
  onShow:function(){
    
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },

  onUnload:function(){
    // 页面关闭

  },
  
  //获得头部滚图和软文的数据
  getSwiperImageList : function() {
  	var that = this;
  	
  	app.getUserOpedId(function() {
  			
  			var params = {
  				method : 'wechat.wisen.text.management.query',
  				openId : app.globalData.userInfo.openid
  			};
  				
  			netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
				
				onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				
				onSuccess: function(resp) {
					
					netUtil.hideLoadingDialog(that);
					
					if(resp.ret == app.globalData.respSuccessCode) {
								
						  var dataArray = resp.data;					  	
						  var swiperImageList = [];
 							var newsItemList = [];
 							for(var i=0;i<dataArray.length;i++) {
 								var dataItem = dataArray[i];
 								if(dataItem.type == 0) {   //如果是滚图
 									swiperImageList.push(dataItem.smallPicture);
 						
 								} else if(dataItem.type == 1) {    //如果是软文
 									var newsItem = {};
 									newsItem.smallPicture = dataItem.smallPicture;
 									newsItem.headline = dataItem.headline;
 									newsItem.linkAddress = dataItem.linkAddress;
 									newsItemList.push(newsItem);
 								}  
 								
 								that.setData({
 									swiperImageList : swiperImageList,
 									newsItemList : newsItemList
 								});
 							
 							}
					
 					} else {
							if(resp.message != undefined && resp.message != null && resp.message != '') {
 								netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 							} else {
 								netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 							}
					}
					
					},
				
					onError: function(res) {
						 console.log("fail");
						 netUtil.hideLoadingDialog(that);
						 netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
					}
				
				}).send();
  				
  		});
  	
  },
	
	hideSmsCodeBox : function() {
		this.setData({
			smsCodeBoxShow : false,
			verifyCode : ''
		})
	},
	
	inputSmsCode: function(e) {

		var verifyCode = e.detail.value;
		this.setData({
			verifyCode : verifyCode
		});
	},
	
	toWantSaveTeaPage :function(e) {
	/*	var that = this;
		
		that.setData({
			pageWhereToGo : 'toWantSaveTeaPage'
		});
		
		app.checkHasRegister('SAVETEA',function(phone) {
 		 	that.setData({
 		 		smsCodeBoxShow : true,
 		 		phoneNum : phone
 		 	});
 		});	*/
 		
 		wx.navigateTo({
			url: '/pages/want-save-tea/want-save-tea'
		});
 		
	},
	
	toWantGetTeaPage : function(e) {
	/*	var that = this;
		
		that.setData({
			pageWhereToGo : 'toWantGetTeaPage'
		});
		
		app.checkHasRegister('MAKETEA',function(phone) {
 		 	that.setData({
 		 		smsCodeBoxShow : true,
 		 		phoneNum : phone
 		 	});
 		});	*/
 		
 		wx.navigateTo({
			url: '/pages/want-get-tea/want-get-tea'
		});
	},
	
	   //点击发送验证码触发的方法
  sendSms:function() {
  	
  	var that = this;
  	
  	//如果已经发送过验证码，一分钟倒计时还没完成，不做处理
  	if(that.data.sendSmsCodeText != '发送验证码') {
  			var secStr = '';

				if(that.data.sendSmsCodeText.length == 6) {
					secStr = that.data.sendSmsCodeText.substring(0, 2);
				} else {
					secStr = that.data.sendSmsCodeText.substring(0, 1);
				}

				var secInt = parseInt(secStr);
				var newSecInt = secInt - 1;

				if(newSecInt <= 0) {

					this.setData({
						sendSmsCodeText: '发送验证码'
					});

					clearInterval(that.data.sendSmsInteval);

					that.setData({
						sendSmsInteval: null
					});

				} else {
					that.setData({
						sendSmsCodeText: newSecInt + '秒后重发'
					});
				}
  		return;
  	}
  	
  	var phoneNum = this.data.phoneNum;
  	if(phoneNum != null && phoneNum.length == 11 && util.isPoneAvailable(phoneNum)) {
  		
  		app.getUserOpedId(function() {
  			
  			var params = {
  				method : 'wechat.wisen.commom.verify.generate',
  				openId : app.globalData.userInfo.openid,
  				data : {
  					phoneNum : that.data.phoneNum
  				}
  			};
  				
  			netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
				
				onPre: function() {
					netUtil.showLoadingDialog(that);
				},
				
				onSuccess: function(resp) {
					
					netUtil.hideLoadingDialog(that);
					
					if(resp.ret == app.globalData.respSuccessCode) {
								
						  	if('发送验证码' == that.data.sendSmsCodeText) {
						  		var text = '60秒后重发';
						  		that.setData({
						  			sendSmsCodeText: text
						  		});

						  		var sendSmsInteval = setInterval(that.sendSms, 1000);

						  		that.setData({
						  			sendSmsInteval: sendSmsInteval
						  		});

						  	} 
						
 						} else {
							if(resp.message != undefined && resp.message != null && resp.message != '') {
 								netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
 							} else {
 								netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
 							}
						}
					
					},
				
					onError: function(res) {
						 console.log("fail");
						 netUtil.hideLoadingDialog(that);
						 netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
					}
				
				}).send();
  				
  		});
  		
  	} else {
  		 netUtil.showAlertDialog("提示", '请输入正确手机号码', false, "确定", null, null);
  	}
  },
  
   //检查验证码是否正确
  checkSmsCode : function(e) {
  	
  	var that = this;
  	
  	var verifyCode = that.data.verifyCode;
  	
  	//发送请求检查验证码
  	if(verifyCode != null && verifyCode != '') {
  		app.getUserOpedId(function() {
  			var params = {
  				method : 'wechat.wisen.commom.verify.check',
  				openId : app.globalData.userInfo.openid,
  				data : {
  					phoneNum : that.data.phoneNum,
  					verifyCode : verifyCode
  				}
  			};
  			
  			netUtil.buildRequest(that,'/wisen/api/wechat/ret',params,{
  				onPre: function() {
						
					},
					
					onSuccess: function(resp) {
						
						//如果验证码正确，不做处理
						if(resp.ret == app.globalData.respSuccessCode) {
							
							that.hideSmsCodeBox();
							
							if(that.data.pageWhereToGo == 'toWantSaveTeaPage') {
								
									wx.navigateTo({
										url: '/pages/want-save-tea/want-save-tea'
									});
								
							} else if (that.data.pageWhereToGo == 'toWantGetTeaPage'  ) {
									wx.navigateTo({
										url: '/pages/want-get-tea/want-get-tea'
									});
							}
						
						
						} else {   //验证码错误，弹出提示框
							netUtil.showAlertDialog("提示",resp.message, false, "确定", null, null);
						}
					},
					
					onError: function(res) {
						 console.log("fail");
						 netUtil.hideLoadingDialog(that);
						 netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
					}
					
  			}).send();
  			
  		});
  	}
  },
  
  toNewsDeatil : function(e) {
  	
  	var newsUrl = e.currentTarget.dataset.newsurl;
  	
  	wx.navigateTo({
				url: '/pages/newsDetail/newsDetail?newsUrl=' + newsUrl
		});
  	
  	
  }
	
})